import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import Layout from '@/components/layout/IndexView.vue'
import {useUserStore} from '@/stores/user'

export const routes = [
    {
        path: '/login',
        component: () => import('@/views/login/indexView.vue'),
        hidden: true
    },
    {
        path: '/',
        component: Layout,
        redirect: '/admin-comment/list',
    },
    {
        path: '/admin-comment',
        component: Layout,
        redirect: '/admin-comment/list',
        meta: { title: '评论操作', icon: 'document' },
        children: [
            {
                path: 'create',
                component: () => import('@/views/admin-comment/SendView.vue'),
                name: 'CreateComment',
                meta: { title: '批量评论' }
            },
            {
                path: 'list',
                component: () => import('@/views/admin-comment/ListView.vue'),
                name: 'AdminCommentList',
                meta: { title: '查看状态' }
            },
            {
                path: 'listDetail/:id',
                component: () => import('@/views/admin-comment/ListDetailView.vue'),
                name: 'AdminCommentListDetail',
                meta: { title: '批次任务详细信息' }
            }
        ]
    },
    {
        path: '/admin-json',
        component: Layout,
        redirect: '/admin-json/list',
        meta: { title: '电影JSON', icon: 'document' },
        children: [
            {
                path: 'list',
                component: () => import('@/views/admin-json/MovieList.vue'),
                name: 'ListJSon',
                meta: { title: '电影列表' }
            },
            {
                path: 'upload',
                component: () => import('@/views/admin-json/MovieUpload.vue'),
                name: 'MovieUpload',
                meta: { title: '电影上传' }
            },
            {
                path: 'template',
                component: () => import('@/views/admin-json/TemplateList.vue'),
                name: 'Template',
                meta: { title: '电影模板管理' }
            }
        ]
    },
    {
        path: '/admin-system',
        component: Layout,
        redirect: '/admin-system/list',
        meta: { title: '系统参数', icon: 'document' },
        children: [
            {
                path: 'list',
                component: () => import('@/views/admin-system/PatternList.vue'),
                name: 'ListPattern',
                meta: { title: '评论匹配规则管理' }
            },
            {
                path: 'config',
                component: () => import('@/views/admin-system/Config.vue'),
                name: 'ListConfig',
                meta: { title: '评论限流规则管理' }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 路由守卫
// router.beforeEach(async (to, from, next) => {
//     const userStore = useUserStore()
//
//     if (userStore.token) {
//         if (to.path === '/login') {
//             next({ path: '/' })
//         } else {
//             // 如果没有用户信息，先获取用户信息
//             if (!userStore.hasUserInfo) {
//                 try {
//                     await userStore.getUserInfo()
//                 } catch (error) {
//                     // 获取用户信息失败，可能是token过期，需要重新登录
//                     await userStore.logout()
//                     next(`/login?redirect=${to.path}`)
//                     return
//                 }
//             }
//             next()
//         }
//     } else {
//         if (to.path === '/login') {
//             next()
//         } else {
//             next(`/login?redirect=${to.path}`)
//         }
//     }
// })

export default router
